package com.rimi.servlet;

import com.rimi.bean.UserBean;
import com.rimi.service.RoleService;
import com.rimi.service.UserService;
import com.rimi.service.impl.RoleServiceImpl;
import com.rimi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.List;

@WebServlet("/v2/UserServlet")
public class UserServlet extends HttpServlet {

    private UserService userService=new UserServiceImpl();
    private RoleService roleService=new RoleServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //动作
        String method = request.getParameter("method");
        if("logout".equals(method)){
            //退出登录
            logout(request,response);
        }else if("update".equals(method)){
            //修改
            update(request,response);
        }else if("pupdate".equals(method)){
            //准备修改
            pupdate(request,response);
        }else{
            //展示用户列表
            List<UserBean> userList = userService.getUserList();
            request.setAttribute("userList",userList);
            request.setAttribute("roleList",roleService.getRoleList());
            request.getRequestDispatcher("/WEB-INF/pages/user/index.jsp").forward(request,response);
        }
    }

    /**
     * 修改
     * @param request
     * @param response
     */
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取修改信息
        String userId = request.getParameter("userId");
        String userStatus = request.getParameter("userStatus");
        String userRole = request.getParameter("userRole");
        String userHeadImg = request.getParameter("userHeadImg");
        //手动封装对象
        UserBean userBean=new UserBean();
        userBean.setUserId(Integer.parseInt(userId));
        userBean.setUserStatus(Integer.parseInt(userStatus));
        userBean.setUserRole(Integer.parseInt(userRole));
        userBean.setUserHeadImg(userHeadImg);
        //
        boolean b = userService.updateUserById(userBean);
        //跳转主页
        response.sendRedirect("/v2/UserServlet");
    }

    /***
     * 准备修改
     * @param request
     * @param response
     */
    private void pupdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        UserBean user = userService.getUserById(Integer.parseInt(id));
        request.setAttribute("user",user);
        request.setAttribute("roleList",roleService.getRoleList());
        request.getRequestDispatcher("/WEB-INF/pages/user/update.jsp").forward(request,response);
    }

    /**
     * 退出登录
     * @param request
     * @param response
     */
    private void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        UserBean user =(UserBean)session.getAttribute("user");
        //用户的cookie销毁
        //保存到用户的浏览器
        Cookie rememberMe = new Cookie("rememberMe","");
        //cookie存活时间
        rememberMe.setPath("/");
        rememberMe.setMaxAge(0);
        response.addCookie(rememberMe);
        //销毁数据库中的token
        userService.dropTokenByUserId(user.getUserId());
        //销毁session
        session.invalidate();
        //跳转登录页面
        response.sendRedirect("/login.jsp");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
